<?php if(!defined('SIDELOADED')) { echo 'Direct access is not allowed.'; }

if(Session::Get('user') > 0) {
  
  $user = new User(Session::Get('user'));
  
  if($user->Perms('admin/keys')) {
    
    $db = new Database();
    
    if(@$_POST) {
      
      $new = strtoupper(md5(microtime() . $user->id));
      
      $db->Insert('users_regkeys', array(
        'key' => $new,
        'creator' => $user->id
      ));
      
    }

    $keys = $db->Query('SELECT * FROM `users_regkeys` WHERE `used` IS NULL;');

    Website::View('page', array(
      'title' => 'Registration keys',
      'content' => Website::View('admin/keys/index', array(
        'keys' => $keys,
        'new' => @$new
      ))
    ), true);
    
  } else {
    
    Website::View('page', array(
      'title' => 'Forbidden',
      'content' => Website::View('error/forbidden')
    ), true);
    
  }
  
} else {
  
  Website::View('page', array(
    'title' => 'Unauthorized',
    'content' => Website::View('error/unauthorized')
  ), true);
  
}
